package cn.knightzz.filter;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 替换掉原始的过滤器 , 使用JSON获取用户名和密码
 *
 * @author knightzz
 * @date 2021/4/10 13:42
 */
@Slf4j
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
        log.debug("CustomAuthenticationFilter......................");
        // 判断请求体是否是json数据
        log.debug(request.getContentType());
        if ((request.getContentType().equals(MediaType.APPLICATION_JSON_UTF8_VALUE))
                || (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE))
                || (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE))) {
              // 用来封装JSON数据
            ObjectMapper mapper = new ObjectMapper();
            UsernamePasswordAuthenticationToken authRequest = null;
            try (ServletInputStream inputStream = request.getInputStream()) {
                // 从数据流中读取JSON数据并封装到map中
                Map<String, String> authenticationBean = mapper.readValue(inputStream, Map.class);
                authRequest = new UsernamePasswordAuthenticationToken(authenticationBean.get("username"),
                        authenticationBean.get("password"));
            } catch (IOException e) {
                e.printStackTrace();
                authRequest = new UsernamePasswordAuthenticationToken(
                        "", "");
            } finally {
                setDetails(request, authRequest);
                return this.getAuthenticationManager().authenticate(authRequest);
            }

        } else {
            return super.attemptAuthentication(request, response);
        }

    }
}
